//
//  GGScrollViewAndCollectionViewController.m
//  Gunterapple
//
//  Created by 热带雨林 on 2020/3/31.
//  Copyright © 2020 热带雨林. All rights reserved.
//

#import "GGScrollViewAndCollectionViewController.h"
#import "GGAutoCollectionFlowLayout.h"
#import "NSArray+URLAddtion.h"
#import "GGAutoScrollCollectionReusableView.h"

static NSString *cellID = @"cellID";
static NSString *headerID = @"headerID";

@interface GGScrollViewAndCollectionViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,reusableViewDelegate>
//@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *redView;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) BOOL isType;
@end

@implementation GGScrollViewAndCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self load];
    self.isType = NO;
    GGAutoCollectionFlowLayout *layout = [[GGAutoCollectionFlowLayout alloc]init];
    
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    self.collectionView = collectionView;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellID];
    [collectionView registerClass:[GGAutoScrollCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerID];
    [self.view addSubview:collectionView];
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}
- (void)load{
     self.dataArray = [NSArray gg_arrayModelWithPlistName:@"moments.plist" withClassName:@"ZFBMomentModel"].mutableCopy;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count * 3;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    cell.backgroundColor = [UIColor blueColor];
    return cell;
}

//collectionView的组头视图 代理方式
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    GGAutoScrollCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerID forIndexPath:indexPath];
    view.delegate = self;
    return view;
}
//通过代理方式重新设置组头视图的约束
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if(self.isType == YES){
        return CGSizeMake(CGRectGetWidth(self.view.frame),500);
    }
    return CGSizeMake(CGRectGetWidth(self.view.frame),300);
}
- (void)changeWithType:(BOOL)type{
    self.isType = type;
    //通过按钮的状态，刷新数据更新headerView的高度 可自定义
    [self.collectionView reloadData];
}

- (NSMutableArray *)dataArray{
    if(!_dataArray){
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
